import Taro from '@tarojs/taro'

interface WeMqttApiResult<T> {
    code: number,
    msg: string,
    data: T
}

export enum DescribeTaskStatus {
    Wait = 1,
    Processing = 2,
    Completed = 3,
    Fail = 4,
    Cancel = 5,
}

export class WeMqttApi {
    private static CloudName: string = 'WeMqttApi'
    private deviceName: string;
    constructor(deviceName: string) {
        this.deviceName = deviceName
    }

    private checkResData<T = any>(r: Taro.cloud.CallFunctionResult): WeMqttApiResult<T> {
        if (r.result) {
            if (typeof r.result === 'string') {
                throw new Error(`未知内容,${r.result}`)
            } else {
                const { code, msg } = r.result
                if (code < 0) throw new Error(msg)
            }
        } else {
            throw new Error(`无返回值`)
        }
        return r.result as WeMqttApiResult<T>
    }

    public async sendText(text: string, speed: number, count: number) {
        const a = await Taro.cloud.callFunction({
            // 要调用的云函数名称
            name: WeMqttApi.CloudName,
            // 传递给云函数的event参数
            data: {
                data: {
                    speed: speed,
                    text: text,
                    count: count
                },
                deviceName: this.deviceName,
                eventCode: 'SendText'
            },
        })
        return this.checkResData(a)
    }

    public async createDevice() {
        const a = await Taro.cloud.callFunction({
            // 要调用的云函数名称
            name: WeMqttApi.CloudName,
            // 传递给云函数的event参数
            data: {
                deviceName: this.deviceName,
                eventCode: 'CreateDevice'
            },
        })
        return this.checkResData(a)
    }

    public async describeDevice() {
        const a = await Taro.cloud.callFunction({
            // 要调用的云函数名称
            name: WeMqttApi.CloudName,
            // 传递给云函数的event参数
            data: {
                deviceName: this.deviceName,
                eventCode: 'DescribeDevice'
            },
        })
        return this.checkResData<DescribeDeviceInfo>(a)
    }

    public async describeDeviceShadow() {
        const a = await Taro.cloud.callFunction({
            // 要调用的云函数名称
            name: WeMqttApi.CloudName,
            // 传递给云函数的event参数
            data: {
                deviceName: this.deviceName,
                eventCode: 'DescribeDeviceShadow'
            },
        })
        const b = this.checkResData<DeviceShadow>(a)
        b.data.parseData = JSON.parse(b.data.Data)
        return b
    }



    public async createLedTaskText(ledTaskId: string) {
        const a = await Taro.cloud.callFunction({
            // 要调用的云函数名称
            name: WeMqttApi.CloudName,
            // 传递给云函数的event参数
            data: {
                ledTaskId: ledTaskId,
                eventCode: 'CreateLedTaskText',
                deviceName: this.deviceName,
            },
        })
        const b = this.checkResData<any>(a)
        return b
    }




    public async describeTask(ledTaskId: string) {
        const a = await Taro.cloud.callFunction({
            // 要调用的云函数名称
            name: WeMqttApi.CloudName,
            // 传递给云函数的event参数
            data: {
                ledTaskId: ledTaskId,
                eventCode: 'DescribeTask',
                deviceName: this.deviceName,
            },
        })
        const b = this.checkResData<any>(a)
        return b
    }


    public async gbkBase64toUtf8(text: string) {
        const a = await Taro.cloud.callFunction({
            // 要调用的云函数名称
            name: WeMqttApi.CloudName,
            // 传递给云函数的event参数
            data: {
                text: text,
                eventCode: 'GbkBase64toUtf8',
                deviceName: this.deviceName,
            },
        })
        const b = this.checkResData<string>(a)
        return b
    }

}